<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>

<div id="manageDialog" class="container-fluid" style="min-height:250px;overflow-x:hidden;overflow-y:auto;">
    <div class="row">
        <form id="createForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label">
                    <span class="help-inline"><font color="red">*</font> </span>
                    伞机管理员：</label>
                <div class="col-sm-5">
                    <div class="radio" class="form-control">
                        <div class="radio radio-inline radio-info">
                            <input id="manage_1" type="radio" name="manage" value="1" <c:if test="${wechatMpGzuser.manage==1}">checked</c:if>>
                            <label for="manage_1">是 </label>
                        </div>
                        <div class="radio radio-inline radio-danger">
                            <input id="manage_0" type="radio" name="manage" value="0" <c:if test="${wechatMpGzuser.manage==0}">checked</c:if>>
                            <label for="manage_0">否 </label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="agent" >
                    关联代理：</label>
                <div class="col-sm-5">
                    <div id="agent" data-id="${agent.id}" data-name="${agent.name}" ></div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-8 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="manageDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#createForm" ).validate( {
            rules: {
                manage: "required"
            },
            messages: {
                manage: "伞机管理员是必填项。"
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    $(function(){
        //初始化树形select控件
        <%--$("#agent").bootstrapCombotree({--%>
            <%--data: ${upmsAgents},//data应符合实例的data格式--%>
            <%--defaultLable:"请选择",--%>
            <%--name : 'agentId',//combotree值域的name，可以用在表单提交--%>
            <%--height:160--%>
        <%--})--%>
        $("#agent").ztreeSelect({
            name:"agentId",
            url: '${basePath}/manage/agentTree',
            params: {isLeaf: 1},
            renderId: "agent",
            callback: function (nodeIds, treeNode) {
            }
        })
    });

    function createSubmit() {
        if(validateForm().form()){
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/gzuser/manage/${wechatMpGzuser.id}',
                data: $('#createForm').serialize(),
                beforeSend: function() {
                    if ($('#manage_1').is(':checked') && $(":input[name='agentId']").val() == '') {
                        infoDialog("管理员必须设置代理。");
                        return false;
                    }
                },
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        manageDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>